package com.donggua.aichebao.commonui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by Administrator on 2017-08-05.
 */
//com.donggua.aichebao.commonui.TwoTextViewLayout
public class TwoTextViewLayout extends LinearLayout {
    protected static final int VERTICAL = 0;
    protected static final int HORIZONTAL = 1;
    protected static final int DEFAULT_SIZE = 15;
    protected static final int DEFAULT_TEXT_COLOR = 0Xff000000;
    protected int orientationStyle = VERTICAL;
    protected int mMargin = 5;
    protected int textSizeType = TypedValue.COMPLEX_UNIT_PX;

    protected TextView tvFirst;
    protected TextView tvSecond;
    public TwoTextViewLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mMargin = (int)context.getResources().getDimension(R.dimen.margin);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.CustomTwoTextView);
        initOrigintation(ta);
        init(context,ta);
        ta.recycle();
    }
    protected void init(Context context,TypedArray ta ){
        int margin = (int)ta.getDimension(R.styleable.CustomTwoTextView_margin,2*mMargin);
        tvFirst =  getTextView(context);
        String firstText = ta.getString(R.styleable.CustomTwoTextView_firstText);
        int firstTextSize = (int)ta.getDimension(R.styleable.CustomTwoTextView_firstTextSize,DEFAULT_SIZE);
        int firstColor = ta.getColor(R.styleable.CustomTwoTextView_firstTextColor,DEFAULT_TEXT_COLOR);
        tvFirst.setText(firstText);
        tvFirst.setTextSize(textSizeType,firstTextSize);
        tvFirst.setTextColor(firstColor);
        LinearLayout.LayoutParams  firstLayoutParams = getMyLayoutParams();
        tvFirst.setLayoutParams(firstLayoutParams);
        Drawable drawable = ta.getDrawable(R.styleable.CustomTwoTextView_firstTextDrawable);
        setFirstDrawable(drawable);
        this.addView(tvFirst);

        String secondText = ta.getString(R.styleable.CustomTwoTextView_secondText);
        int secondTextSize = (int)ta.getDimension(R.styleable.CustomTwoTextView_secondTextSize,DEFAULT_SIZE);
        int secondColor = ta.getColor(R.styleable.CustomTwoTextView_secondTextColor,DEFAULT_TEXT_COLOR);
        tvSecond =  getTextView(context);
        tvSecond.setText(secondText);
        tvSecond.setTextSize(textSizeType,secondTextSize);
        tvSecond.setTextColor(secondColor);
        LinearLayout.LayoutParams  secondLayoutParams = getMyLayoutParams(orientationStyle,margin);
        tvSecond.setLayoutParams(secondLayoutParams);
        this.addView(tvSecond);
    }
    protected void setFirstDrawable(Drawable drawable){
        if(drawable != null){
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
            tvFirst.setCompoundDrawables(drawable,null,null, null);
            tvFirst.setCompoundDrawablePadding(2*mMargin);
        }
    }
    protected  void initOrigintation(TypedArray ta){
        orientationStyle =  ta.getInt(R.styleable.CustomTwoTextView_textLayoutStyle,VERTICAL);
        if(orientationStyle == VERTICAL){
            this.setOrientation(LinearLayout.VERTICAL);
            this.setGravity(Gravity.CENTER_HORIZONTAL);
        }else{
            this.setOrientation(LinearLayout.HORIZONTAL);
            this.setGravity(Gravity.CENTER_VERTICAL);
        }
    }
    private TextView getTextView(Context context) {
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(textSizeType,DEFAULT_SIZE);
        return textView;
    }
    protected  LinearLayout.LayoutParams getMyLayoutParams(){
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        return layoutParams;
    }

    protected  LinearLayout.LayoutParams getMyLayoutParams(int originStyle,int margin){
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if(originStyle == HORIZONTAL){
            layoutParams.leftMargin = margin;
        }else{
            layoutParams.topMargin = margin;
        }
        return layoutParams;
    }

    public void setFirstText(String text){
        tvFirst.setText(text);
    }

    public void setSecondText(String text){
        tvSecond.setText(text);
    }

    public void setFisrtTextColor(int color){
        tvFirst.setTextColor(color);
    }

    public void setSecondTextColor(int color){
        tvSecond.setTextColor(color);
    }
}
